package com.globallogic.jira.gadget.servlet;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.config.ConstantsManager;
import com.atlassian.jira.exception.CreateException;
import com.atlassian.jira.issue.issuetype.IssueType;
import com.atlassian.jira.issue.link.IssueLinkTypeManager;
import com.globallogic.jira.gadget.utils.IssueLinkTypes;
import com.globallogic.jira.gadget.utils.IssueTypes;

/**
 * Context listener creates needed issue types and link types
 * 
 * @author anton.pereverziev
 * 
 */

public class CreatingTypesListener implements ServletContextListener {
    private static final Logger log = Logger
	    .getLogger(CreatingTypesListener.class);

    public void contextDestroyed(ServletContextEvent event) {
	// TODO Auto-generated method stub

    }

    public void contextInitialized(ServletContextEvent event) {
	createIssueTypes();
	createLinkTypes();
    }

    /**
     * Create needed issue types
     */
    private void createIssueTypes() {

	log.debug("Creating types in listener");

	boolean businessRequirementTypeExist = false;
	boolean testCaseTypeExist = false;
	ConstantsManager constantsManager = ComponentManager.getInstance()
		.getConstantsManager();
	for (IssueType it : constantsManager.getAllIssueTypeObjects()) {
	    log.debug(it.getName());
	    if (it.getName().equals(IssueTypes.BUSINESS_REQUIREMENT.typeName)) {
		businessRequirementTypeExist = true;
		log.debug("Found " + IssueTypes.BUSINESS_REQUIREMENT.typeName);
	    } else if (it.getName().equals(IssueTypes.TEST_CASE.typeName)) {
		testCaseTypeExist = true;
		log.debug("Found " + IssueTypes.TEST_CASE.typeName);
	    }
	}
	if (!businessRequirementTypeExist) {

	    try {
		constantsManager.insertIssueType(
			IssueTypes.BUSINESS_REQUIREMENT.typeName, 1000L, null,
			IssueTypes.BUSINESS_REQUIREMENT.description,
			"/images/icons/genericissue.gif").getId();

	    } catch (CreateException e) {
		e.printStackTrace();
	    }

	    log.debug("Created new type "
		    + IssueTypes.BUSINESS_REQUIREMENT.typeName);
	}

	if (!testCaseTypeExist) {
	    try {
		constantsManager.insertIssueType(IssueTypes.TEST_CASE.typeName,
			1001L, null, IssueTypes.TEST_CASE.description,
			"/images/icons/genericissue.gif").getId();
	    } catch (CreateException e) {
		e.printStackTrace();
	    }

	    log.debug("Created new type " + IssueTypes.TEST_CASE.typeName);
	}
	for (IssueType it : constantsManager.getAllIssueTypeObjects()) {
	    log.debug(it.getName());
	    if (it.getName().equals(IssueTypes.BUSINESS_REQUIREMENT.typeName)
		    && !businessRequirementTypeExist) {
		ComponentManager.getInstance().getIssueTypeSchemeManager()
			.addOptionToDefault(it.getId());
	    } else if (it.getName().equals(IssueTypes.TEST_CASE.typeName)
		    && !testCaseTypeExist) {
		ComponentManager.getInstance().getIssueTypeSchemeManager()
			.addOptionToDefault(it.getId());
	    }
	}

    }

    private void createLinkTypes() {
	IssueLinkTypeManager issueLinkTypeManager = ComponentManager
		.getComponentInstanceOfType(IssueLinkTypeManager.class);

	issueLinkTypeManager.createIssueLinkType(
		IssueLinkTypes.TREE_BUILD_LINK.typeName,
		IssueLinkTypes.TREE_BUILD_LINK.outward,
		IssueLinkTypes.TREE_BUILD_LINK.inward,
		IssueLinkTypes.TREE_BUILD_LINK.style);
    }

}